USE [DSRSystem_Temp]
GO

/****** Object:  StoredProcedure [dbo].[spGetIndicatorTickboxesByCompanyIds]    Script Date: 1/26/2016 12:55:59 PM ******/
DROP PROCEDURE [dbo].[spGetIndicatorTickboxesByCompanyIds]
GO

/****** Object:  StoredProcedure [dbo].[spGetIndicatorTickboxesByCompanyIds]    Script Date: 1/26/2016 12:55:59 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[spGetIndicatorTickboxesByCompanyIds]
	@CompanyIds NVARCHAR(MAX)
AS
BEGIN
	SET NOCOUNT ON;

	DECLARE @XML XML
	SET @XML = CONVERT(XML, @CompanyIds, 1)

	DECLARE @SelectedCompanies TABLE
	(
		Id uniqueidentifier
	)
	INSERT INTO @SelectedCompanies
	SELECT TID.id.value('.','uniqueidentifier') AS Id
	FROM @XML.nodes('/CompanyIds') AS T(c) outer apply T.c.nodes('Id') as TID(id)

	SELECT base.[IndicatorId]
		  ,base.[CompanyId]
		  ,base.[IndicatorTemplateId]
		  ,base.[TickboxCode]
		  ,base.[TickboxText] 
		  ,base.[IsChecked]

	FROM [dbo].[IndicatorTickboxes] AS base
	INNER JOIN @SelectedCompanies AS SelectedCompanies ON SelectedCompanies.Id =   base.CompanyId

END

GO


